package com.ycz.springcloud.service.impl;/*
 @author ycz
 @date 2021-08-19-13:41  
*/

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.ycz.springcloud.form.SysUserResetPwdForm;
import com.ycz.springcloud.query.BusCustomerQuery;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ycz.springcloud.common.CodeMsg;
import com.ycz.springcloud.common.Constant;
import com.ycz.springcloud.common.Result;
import com.ycz.springcloud.domain.SysUser;
import com.ycz.springcloud.form.SysUserForm;
import com.ycz.springcloud.mapper.SysUserMapper;
import com.ycz.springcloud.query.SysUserQuery;
import com.ycz.springcloud.service.SysUserService;
import com.ycz.springcloud.vo.SysUserVo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public Result queryPage(SysUserQuery query) {
        //开启分页查询
        Page<SysUserVo> sysUserVOPage = PageHelper.startPage(query.getPage(), query.getLimit());
        //根据参数查询用户列表
        sysUserMapper.selectList(query);
        return new Result(sysUserVOPage.toPageInfo());
    }

    @Override
    public List<SysUserVo> selectList(SysUserQuery query) {
        return sysUserMapper.selectList(query);
    }


    @Override
    public SysUser queryUserByName(String username) {
        return sysUserMapper.queryUserByName(username);
    }

    @Override
    public Result add(SysUserForm sysUserForm) {
        SysUserQuery query=new SysUserQuery();

        query.setLoginName(sysUserForm.getLoginName());

        SysUserVo sysUserVo= sysUserMapper.selectUserByPhone_loginName_idCard(query);
        if (sysUserVo!=null){
            return new Result(CodeMsg.USER_LOGIN_NAME_EXIST_ERROR);
        }

        query=new SysUserQuery();
        query.setPhone(sysUserForm.getPhone());
        sysUserVo=sysUserMapper.selectUserByPhone_loginName_idCard(query);
        if (sysUserVo!=null){
            return new Result(CodeMsg.CUSTOMER_PHONE_EXIST_ERROR);
        }

        query=new SysUserQuery();
        query.setIdCard(sysUserForm.getIdCard());
        sysUserVo=sysUserMapper.selectUserByPhone_loginName_idCard(query);
        if (sysUserVo!=null){
            return new Result(CodeMsg.USER_ID_CARD_EXIST_ERROR);
        }

        Md5Hash md5Hash = new Md5Hash(Constant.DEFAULT_PASSWORD, Constant.MD5_SALT, 2);
        sysUserForm.setLoginPassword(md5Hash.toString());

        sysUserMapper.insertUser(sysUserForm);

        return new Result();
    }

    @Override
    public List<SysUserVo> selectList2(SysUserQuery query) {
        /**
         * 分页后的
         */
        return sysUserMapper.selectList2(query);
    }

    @Override
    public Integer getUserCount(SysUserQuery query) {
        /**
         * 获得搜索之后的数量总数
         */
        return sysUserMapper.getUserCount(query);
    }

    @Override
    public void exportUser(SysUserQuery query, HttpServletResponse response) throws IOException {
        //与客户端相关的输出流
        ServletOutputStream os=response.getOutputStream();
        //response为HttpServletResponse对象
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
        String fileName= URLEncoder.encode("客户信息.xls","utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+fileName);
        //符合条件的客户
        List<SysUserVo> sysUserVos= sysUserMapper.selectList(query);
        System.out.println(sysUserVos);
        //将客户转化为excel数据流
        // 通过工具类创建writer，默认创建xls格式
        ExcelWriter writer = ExcelUtil.getWriter();
        //取别名
        writer.addHeaderAlias("id","编号");
        writer.addHeaderAlias("loginName","用户名");
        writer.addHeaderAlias("realname","姓名");
        writer.addHeaderAlias("phone","手机号码");
        writer.addHeaderAlias("idCard","身份证");
        writer.addHeaderAlias("sex","性别");
        writer.addHeaderAlias("address","地址");
        writer.addHeaderAlias("createTime","创建时间");
        writer.addHeaderAlias("img","照片");
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(sysUserVos,true);
        //将Excel输出给浏览器
        writer.flush(os,true);
        System.out.println("  //将Excel输出给浏览器");
    }

    @Override
    public void batchInsert(List<SysUserVo> sysUserVos) {
        sysUserMapper.batchInsert(sysUserVos);
    }

    @Override
    public void deleteUser(List<Integer> ids) {
        sysUserMapper.deleteUser(ids);
    }

    @Override
    public Result updatePassword(SysUserResetPwdForm form) {
        System.out.println("进来了IMPL 的修改密码");
        /*
        判断是否新密码与旧密码相同
         */
        if (form.getPassword().equals(form.getUsedPassword())) {//新密码与旧密码
            return new Result(CodeMsg.USEDPASSWORD_equal_NEWPASSWORD);
        }
        /*
        判断新密码之间是否相同
         */
        if (!form.getPassword().equals(form.getNewPassword())) {
            return new Result(CodeMsg.PASSWORD_NO_EQUAL_NEWPASSWORD);
        } else {
             /*
        获取原来的密码
         */
            String usedPassword = sysUserMapper.selectPasswordByID(form.getId());

            System.out.println("数据库原来的密码是" + usedPassword);
        /*
        将sysUserResetPwdFrom原来的密码加密看看是否一样
         */
            Md5Hash md5Hash = new Md5Hash(form.getUsedPassword(), Constant.MD5_SALT, 2);

            System.out.println("用户输入的原来密码为" + md5Hash.toString());

            if (usedPassword.equals(md5Hash.toString())) {
                System.out.println("两个旧密码之间相同，然后插入新密码即可");
            /*
            将新密码加密
             */
                Md5Hash md5Hash2 = new Md5Hash(form.getPassword(), Constant.MD5_SALT, 2);
                System.out.println("修改后的密码加密后" + md5Hash2.toString());
                /*
                更新新密码
                 */
                sysUserMapper.updatePassword(form.getId(), md5Hash2.toString());

            }
            return new Result();

        }

    }

}